package com.meichis.yndms.adapter.RecycleViewItemType;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meichis.mcsappframework.adapter.recyclerview.base.ItemViewDelegate;
import com.meichis.mcsappframework.adapter.recyclerview.base.ViewHolder;
import com.meichis.yndms.R;
import com.meichis.yndms.model.InspectTemplate_Item;

import java.util.List;


public class FillMode_TDPNew implements ItemViewDelegate<InspectTemplate_Item>, View.OnClickListener {

    private List<InspectTemplate_Item> items;
    private Context context;

    public FillMode_TDPNew(Context context, List<InspectTemplate_Item> items) {
        this.items = items;
        this.context = context;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.fill_mode_tdp_new;
    }

    @Override
    public boolean isForViewType(InspectTemplate_Item item, int position) {
        return item.getItem().getFillMode() == 22;
    }

    @Override
    public void convert(ViewHolder holder, InspectTemplate_Item item, int position) {
        TextView tv_group = holder.getView(R.id.tv_group);
        View v_bottom = holder.getView(R.id.v_bottom);
        if (position == 0) {
            if (!TextUtils.isEmpty(item.getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
            }
        } else {
            if (!TextUtils.isEmpty(item.getItemGroup()) && !item.getItemGroup().equals(items.get(position - 1).getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
            }
        }

        if ((position + 1) == items.size()) {//如果是最后一个，则显示底部圆角边框
            v_bottom.setVisibility(View.VISIBLE);
        } else {
            //如果当前位置的itemGroup 与下一个位置的itemGroup 不同 ：不是同一个分类
            if (!TextUtils.isEmpty(item.getItemGroup()) && !item.getItemGroup().equals(items.get(position + 1).getItemGroup())) {
                v_bottom.setVisibility(View.VISIBLE);
            } else {
                v_bottom.setVisibility(View.GONE);
            }
        }
        TextView tv_answer = holder.getView(R.id.tv_answer);
        TextView tv_question = holder.getView(R.id.tv_question);
        tv_question.setText(Html.fromHtml(String.valueOf(position + 1) + ". " +
                item.getItem().getName().replace("<", "&lt;") +
                (!TextUtils.isEmpty(item.getItem().getUnit()) ? "(" + item.getItem().getUnit() + ")" : "") +
                (item.getIsMustFill() == 1 ? " <font  color=\"#FF0000\">*</font>" : "") +
                (!TextUtils.isEmpty(item.getItem().getSubTitle()) ? "<br/> <font color=\"#8D8D8D\" ><small>" + item.getItem().getSubTitle() + "</small></font>" : "")
        ));
        tv_answer.setText(item.getFillName());
        tv_answer.setTag(position);
        tv_answer.setOnClickListener(this);

        LinearLayout ll = holder.getView(R.id.ll_answer);
        setBackground(item, ll);
    }

    @Override
    public void onClick(View view) {
//        int position = (Integer) view.getTag();
//        Intent intent = new Intent(context, TDPListActivity.class);
//        intent.putExtra("Position",position);
//        context.startActivity(intent);
    }

    private void setBackground(InspectTemplate_Item item, LinearLayout ll) {
        ll.setBackgroundResource(item.getFillName().length() == 0 && item.getIsMustFill() == 1 ? R.drawable.bg_ll_frame_red : R.drawable.bg_ll_frame);
    }
}
